package com.njym.controller;

import com.njym.entity.PlatformBusinessRec;
import com.njym.payment.alipay.properties.getDataParams;
import com.njym.service.PlatformBusinessRecService;
import com.njym.vo.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author zhy
 * @date 2019-11-01 14:52
 **/
@RestController
@RequestMapping("/sys_tradeData")
public class GetTradeData {
	private static final Logger log = LoggerFactory.getLogger(GetTradeData.class);

	@Resource
	private PlatformBusinessRecService platformBusinessRecService;

	@PostMapping("/insertData")
	public Json smilePay(@Valid getDataParams prepayParams, BindingResult bindingResult) {
		String oper = "smilePayMateInit";
		if (bindingResult.getErrorCount() > 0) {
			return Json.fail(oper, bindingResult.getFieldErrors().get(0).getDefaultMessage());
		}
		// 初始化数据到数据库
		PlatformBusinessRec businessRec = new PlatformBusinessRec();
		businessRec.setOrgId(prepayParams.getOrg_id());
		businessRec.setChannelId(prepayParams.getChannel_id());
		businessRec.setModeId(prepayParams.getMode_id());
		businessRec.setRecType(Byte.valueOf(prepayParams.getRec_type()));
		businessRec.setOutTradeNo(prepayParams.getOut_trade_no());
		businessRec.setTradeNo(prepayParams.getTrade_no());
		businessRec.setCardNo(prepayParams.getCardNo());
		businessRec.setName(prepayParams.getName());
		businessRec.setTradeType(prepayParams.getTrade_type());
		businessRec.setTradeStatus(prepayParams.getTrade_status());
		businessRec.setRemarks(prepayParams.getRemarks());
		businessRec.setOutRequestNo(prepayParams.getOut_trade_no());
		BigDecimal bd = new BigDecimal(prepayParams.getTotal_fee());
		//设置小数位数，第一个变量是小数位数，第二个变量是取舍方法(四舍五入)
		bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
		businessRec.setTotalFee(bd);
		businessRec.setTradeDate(new Date());
		businessRec.setAccountFlag(Byte.valueOf("0"));
		businessRec.setAbnormalFlag(Byte.valueOf("0"));
		businessRec.setCreateDate(new Date());
		businessRec.setRemarks(prepayParams.getRemarks());
		businessRec.setOperatorId(prepayParams.getOperatorId());
		businessRec.setOperatorName(prepayParams.getOperatorName());
		boolean num = platformBusinessRecService.insert(businessRec);
		if (num) {
			return Json.succ();
		} else {
			return Json.fail();
		}
	}
}
